package com.zs.toolbox.web2j.system.core.service.impl;

import com.github.cage.Cage;
import com.github.cage.GCage;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.system.message.domain.request.WtSendEmailRequest;
import com.zs.toolbox.web2j.system.message.domain.request.WxSendSmsVcodeRequest;
import com.zs.toolbox.web2j.system.message.domain.service.WtMessageClientService;
import com.zs.toolbox.web2j.system.core.config.cache.WtUserCache;
import com.zs.toolbox.web2j.system.core.constant.WtEVcodePurpose;
import com.zs.toolbox.web2j.system.core.service.WtVcodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.util.Arrays;

/**
 * @author shuai.zhang  on 2018/9/26
 * @description
 */
@Service
@Slf4j
public class WtVcodeServiceImpl implements WtVcodeService {
    @Autowired
    private WtUserCache userCache;

    /*图片生成工具*/
    Cage imageGenergator = new GCage();


    @Autowired
    private WtMessageClientService messageClientService;


    @Override
    public String newImageCode(String key) {
        String code = RandomStringUtils.randomAlphanumeric(4);
        userCache.saveSendImageVcode(key, code);
        log.info("生成图片验证码 {} {}",key ,code);
        return Base64Utils.encodeToString(imageGenergator.draw(code));
    }

    @Override
    public void verifyImageCode(String key, String imageCode) {
        String cacheKeyCode = userCache.getSendImageVcode(key);
        log.debug("图片验证码 key{} cache {} ，req {}", key, cacheKeyCode, imageCode);
        if(!imageCode.equalsIgnoreCase(cacheKeyCode)) {
            throw WtExceptions.E_VCODE;
        }
    }

    @Override
    public void newEmailVcode(WtEVcodePurpose vcodeType, String email) {
        String vcode = RandomStringUtils.randomNumeric(6, 7);
        WtSendEmailRequest request = new WtSendEmailRequest();
        request.setEmails(Arrays.asList(email));
        request.setContent(String.format("您正在进行%s操作，注册验证码为: %s", vcodeType.getMessage(),vcode));
        request.setTitle(String.format("验证码消息提示"));
        log.debug("email {} 生成 注册验证码 {}", email, vcode);
        userCache.saveSendVcode(email,vcode);
        messageClientService.sendEmail(request);
    }

    @Override
    public void newPhoneVcode(WtEVcodePurpose wtEVcodePurpose, String phone) {
        String vcode = RandomStringUtils.randomNumeric(6, 7);
        WxSendSmsVcodeRequest reqeust = new WxSendSmsVcodeRequest();
        reqeust.setPhone(phone);
        reqeust.setVcode(vcode);
        log.debug("phone {} 生成 注册验证码{}", phone, vcode);
        switch (wtEVcodePurpose) {
            case VCODE_PURPOSE_LOGIN:
                messageClientService.sendPhoneLoginVCodeSms(reqeust);
                break;
            case VCODE_PURPOSE_REGISTER:
                messageClientService.sendPhoneRegisterVCodeSms(reqeust);
                break;
            case VCODE_PURPOSE_FINDPASSWORD:
                messageClientService.sendPhoneFindPasswordVCodeSms(reqeust);
                break;
            default:
                break;
        }
        userCache.saveSendVcode(phone,vcode);
    }

    @Override
    public void verifyEmailOrPhoneVcode(String key, String vcode) {
        String sendVcode = userCache.getSendVcode(key);
        if(!vcode.equalsIgnoreCase(sendVcode)) {
            throw WtExceptions.E_VCODE;
        }
    }

    @Override
    public void clearCacheSendCode(String key) {
        userCache.clearSendVcode(key);
    }

    @Override
    public void clearCacheImageCode(String imageCodeKey) {
        userCache.clearSendImageVcode(imageCodeKey);
    }
}
